package com.example.controller;

import com.example.config.RabbitmqConfig;
import com.example.model.OrderModel;
import com.example.service.OrderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author lbq
 * @version 1.0
 * @date 2021/6/27 20:49
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private AmqpTemplate rabbitmqTemplate;

    @PostMapping("/save")
    public Long save() {
        OrderModel orderModel = new OrderModel();
        orderModel.setStatus(0);
        orderModel.setCreateTime(new Date());
        orderModel.setUpdateTime(new Date());
        orderService.save(orderModel);
        System.out.println("创建订单成功, " + orderModel.toString());
        rabbitmqTemplate.convertAndSend(RabbitmqConfig.ORDER_STATUS_EXCHANGE,
                RabbitmqConfig.ORDER_STATUS_QUEUE_ROUTING_KEY,
                orderModel.getId());
        return orderModel.getId();
    }

    @PostMapping("/pay")
    public String pay(@RequestParam(name = "id") Long id) {
        return orderService.pay(id);
    }

}
